package world.hezi.pools;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;
/*jedis工具类*/

public class JedisPools {
    private JedisPools() {}
    private static final redis.clients.jedis.JedisPool JEDIS_POOL;
    private static final int MAX_TOTAL;
    private static final int MAX_WAIT_MILLIS;
    private static final String HOST;
    private static final int PORT;
    private static final String PASSWORD;
    private static final int DATABASE;

    static{
        ResourceBundle rb = ResourceBundle.getBundle("jedis");
        MAX_TOTAL = Integer.parseInt(rb.getString("maxTotal"));
        MAX_WAIT_MILLIS = Integer.parseInt(rb.getString("maxWaitMillis"));
        HOST = rb.getString("host");
        PORT = Integer.parseInt(rb.getString("port"));
        PASSWORD = rb.getString("password");
        DATABASE = Integer.parseInt(rb.getString("database"));
    }

    static{
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(MAX_TOTAL);
        jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
        JEDIS_POOL = new redis.clients.jedis.JedisPool(jedisPoolConfig, HOST, PORT,2000, PASSWORD, DATABASE);
    }

    public static Jedis getJedis(){
        return JEDIS_POOL.getResource();
    }

    public static void close(Jedis jedis){
        if(jedis!=null){
            jedis.close();
        }
    }
}